//
//  DestinationCollectViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "DestinationCollectViewController.h"
#import "DestinationDetailViewController.h"
#import "DestinationScenicModel.h"
#import "DestinationDatabaseManager.h"

@interface DestinationCollectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation DestinationCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // [self setUpData];
    [self addSubViews];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    [self setUpData];
    [self.tableView reloadData];
}


#pragma mark -- 设置导航栏

- (void)setUpData
{

     self.dataArray = [[DestinationDatabaseManager shareManager] selectAllModels];
}

- (void)addSubViews
{
    UIImageView *backgroundImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    backgroundImg.image = [UIImage imageNamed:@"MineBackGround.jpg"];
    backgroundImg.userInteractionEnabled = YES;
    [self.view addSubview:backgroundImg];

    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    self.tableView.dataSource = self;
    self.tableView.delegate= self;
    self.tableView.alpha = 0.8;
    [backgroundImg addSubview:self.tableView];
}

#pragma mark -- UITableViewDataSource协议


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    if (self.dataArray.count > 0) {
                DestinationScenicModel *model = self.dataArray[indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@", model.name];
                cell.detailTextLabel.numberOfLines = 0;
                cell.detailTextLabel.text = [NSString stringWithFormat:@"     %@",model.recommended_reason];
       
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark -- 跳转界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 选中cell后不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
            DestinationScenicModel *model = self.dataArray[indexPath.row];
        DestinationDetailViewController *ddVC = [[DestinationDetailViewController alloc] init];
        ddVC.model = model;
        [self.navigationController pushViewController:ddVC animated:YES];
    
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 数据库中删除数据
    DestinationScenicModel *model = self.dataArray[indexPath.row];
    [[DestinationDatabaseManager shareManager] deleteByName:model.name];
    // 数组中删除数据
    [self.dataArray removeObject:model];
    // 刷新列表
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}


- (void)actionLeftButton:(UIBarButtonItem *)rightButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
